package zmwxapplet

import (
	"github.com/gin-gonic/gin"
	"github.com/gogap/logs"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/room"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)

// GetDeviceInfo 获取设备详情-不需要登录
func GetDeviceInfo(ctx *gin.Context) {
	param := &struct {
		Sn        string `form:"deviceSn" binding:"required"`
		AppId     string `form:"appId" binding:"required"`
		AppSecret string `form:"appSecret" binding:"required"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	if param.AppId != "cnmanyd" && param.AppSecret != "c0fa9b7e25b0ae49c32e97903526004ce4b0e851a3940f5f921fd135a5642418" {
		Response(ctx)("非法请求-路径不存在", CodeError, "路径不存在")(Error)
		return
	}
	devInfo := logic.NewDeviceLogic().WXGetDeviceInfoByDeviceSn(param.Sn)
	Response(ctx)("", CodeSuccess, devInfo)(OK)
}

// GetDevInfo 获取设备的详情信息
func GetDevInfo(ctx *gin.Context) {
	param := &struct {
		Sn string `form:"deviceSn" binding:"required"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	devInfo := logic.NewDeviceLogic().GetDeviceInfoByDeviceSn(param.Sn)
	Response(ctx)("", CodeSuccess, devInfo)(OK)
}

// GetRoomDevInfo 获取设备的详情信息
func GetRoomDevInfo(ctx *gin.Context) {
	param := &struct {
		Sn string `form:"deviceSn" binding:"required"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	devInfo := logic.NewDeviceLogic().GetDeviceInfoByDeviceSn(param.Sn)

	roomData := dao.NewRoomDaoDao().GetRoomDetails(devInfo.PlaceId, devInfo.Room).Unwrap().(*room.Room)
	devInfo.WifiName = roomData.WifiName
	devInfo.WifiPassword = roomData.WifiPassword

	paramAutomate := &request.GetAutomateGoodsBindListReq{
		GoodsStatus: helper.TypeNumGoodsStatusYesOn,
		//Status:      helper.TypeNumCommonStatusTrue,
		DeviceSn: devInfo.DeviceSn,
	}

	if paramAutomate.DeviceSn != "" {
		devInfo.AutomateGoodsList = dao.NewAutomateDaoDao().GetAutomateGoodsBindList(paramAutomate).Unwrap()
	}
	Response(ctx)("", CodeSuccess, devInfo)(OK)
}

// GetDevicePassword 获取设备密码
func GetDevicePassword(ctx *gin.Context) {
	// 请求参数处理
	param := &request.GetPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	if param.ProductId == "" {
		orderInfo := dao.NewOrderDao().GetOrderInfoByTradeNo(param.TradeNo).Unwrap().(*orders.OrderModel)
		param.ProductId = orderInfo.ProductId
	}
	// 获取密码业务逻辑
	pwd, useTime := logic.NewDeviceLogic().HandleLogicGetPassword(param)

	Response(ctx)("", CodeSuccess, struct {
		Password string `json:"password"` // 密码
		UseTime  int64  `json:"useTime"`  // 使用时长
		Count    int    `json:"count"`    // 密码重置次数
	}{
		Password: pwd,
		UseTime:  useTime,
		Count:    0,
	})(OK)
}

// DevPasswordVerifyCRC16 设备密码 CRC16校验
func DevPasswordVerifyCRC16(ctx *gin.Context) {
	param := &request.DevPasswordVerifyCRC16Req{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	//const key = "170802"

	code := utils.CRC16(utils.StrToArrayBuffer(param.Message))
	logs.Info(param.Message, code)
	Response(ctx)("", CodeSuccess, code)(OK)
}

// ResetDevPassword 重置设备密码
func ResetDevPassword(ctx *gin.Context) {
	// 请求参数处理
	data := &request.GetPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(data)).Unwrap()
	// 重置密码业务逻辑
	pwd, useTime := logic.NewDeviceLogic().HandleLogicGetPassword(data)

	Response(ctx)("", CodeSuccess, struct {
		Password string `json:"password"`
		UseTime  int64  `json:"useTime"` // 使用时长
		Count    int    `json:"count"`   // 密码重置次数
	}{
		Password: pwd,
		UseTime:  useTime,
		Count:    0,
	})(OK)
}

// GetDeviceUseTime 获取设备使用时长
func GetDeviceUseTime(ctx *gin.Context) {
	// 请求参数处理
	param := &request.GetPasswordReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	_, useTime := logic.NewDeviceLogic().HandleUseTime(param)

	// 获取密码使用时长
	Response(ctx)("", CodeSuccess, useTime)(OK)
}
